<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{pay_totalamount}}</text>
		</view>

		<view class="pay-type-list">

			<view class="type-item b-b" @click="changePayType('wechat')">
				<text class="icon yticon icon-weixinzhifu"></text>
				<view class="con">
					<text class="tit">微信支付</text>
					<text>推荐使用微信支付</text>
				</view>
				<label class="radio">
					<radio value="wechat" color="#fa436a" :checked='pay_type == "wechat"' />
					</radio>
				</label>
			</view>
			<!-- #ifdef APP-PLUS -->
			
			
			<view class="type-item b-b" @click="changePayType('alipay')">
				<text class="icon yticon icon-alipay"></text>
				<view class="con">
					<text class="tit">支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="alipay" color="#fa436a" :checked='pay_type == "alipay"' />
					</radio>
				</label>
			</view>
			<!-- #endif -->  
			
			<view class="type-item" @click="changePayType('balance')">
				<text class="icon yticon icon-erjiye-yucunkuan"></text>
				<view class="con">
					<text class="tit">预存款支付</text>
					<text>可用余额 ¥{{userinfo.money}}</text>
				</view>
				<label class="radio">
					<radio value="balance" color="#fa436a" :checked='pay_type == "balance"' />
					</radio>
				</label>
			</view>
		</view>

		<text class="mix-btn" @click="payit">确认支付</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pay_type: "wechat",
				pay_totalamount: 0,
				pay_channel: 'app', //app,miniapp
				userinfo: {},
				ordertable: 'jyshop_pinorder_product',
				orderid: 0,
				orderInfo: {}
			};
		},
	
		onLoad(options) {
			this.orderid = options.orderid || 0;
			this.ordertable = options.ordertable || 'jyshop_order_product';
			// #ifdef MP-WEIXIN
			this.pay_channel = "miniapp";
			// #endif
			// #ifdef APP-PLUS
			  this.pay_channel="app";
			// #endif
			this.getUserinfo();
			this.getPayInfo();
		},

		methods: {
			//获得余额
			getUserinfo() {
				let that = this;
				this.$util.req("/user/getinfo", {}, true, function(res) {
					that.userinfo = res.data;
	 
					
				});
			},
			getPayInfo() {
				let that = this;
				this.$util.req("/order/getOrderPayAmount", {
					id: this.orderid,
					ordertable: this.ordertable
				}, true, function(res) {
					that.pay_totalamount = res.data.pay_totalamount;
					that.pay_type = res.data.pay_type;
					 
				});
			},

			//选择支付方式
			changePayType(type) {
				this.pay_type = type;
			},
			//确认支付
			payit: async  function() {
				let params = {
					pay_channel: this.pay_channel,
					orderid: this.orderid,
					pay_type: this.pay_type
				};
				let that = this;
				let requrl = this.ordertable == "jyshop_pinorder_product" ? "/orderpin/payOrder" :
					"/order/payorder";
				 
				 let [errmsg,resdata]= await this.$util.req(requrl, params, true);
			     let result = resdata.data;
				 if(result.code !=1){
					 uni.showToast({
					 	title:result.msg
					 });
					 return ;
				 }
				 this.payresult(result.data);
				// uni.redirectTo({
				// 	url: '/pages/money/paySuccess'
				// })
			},
			payresult(resdata) {
					let that= this;
					let paysuccess_url= '/pages/money/paySuccess?orderid='+ this.orderid+"&ordertable="+this.ordertable;
					 
				if (this.pay_type == "balance") { //余额支付，直接成功
					uni.redirectTo({
						url: paysuccess_url
					});
					return;
				}
			

				if (this.pay_channel == "app") { // app 支付，支持微信和支付宝
					let provider = this.pay_type == "wechat" ? 'wxpay' : 'alipay';
					uni.requestPayment({
						provider: provider,
						orderInfo: resdata.paystr,
						success(res) {
							uni.redirectTo({
								url: paysuccess_url
							});
						},
						fail(err) {
						console.log("失败:"+JSON.stringify(err));
						//	that.$util.toast("失败:"+);
						}
					})
				}else if (this.pay_channel == "miniapp") { //微信小程序，只有微信支付
				
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: resdata.paystr.timeStamp,
						nonceStr: resdata.paystr.nonceStr,
						package: resdata.paystr.package,
						signType: resdata.paystr.signType,
						paySign: resdata.paystr.paySign,
						success(res) {
							uni.redirectTo({
								url: paysuccess_url
							});
						},
						fail(err) {
						console.log("失败:"+JSON.stringify(err));
						}
					})
				}

			}
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
